package com.us.usws.service.cud.impl;

import java.util.List;

import javax.inject.Inject;

import net.iwsh.core.exception.DataConflictException;

import com.us.usws.common.SiteConstants;
import com.us.usws.dao.DeptDAO;
import com.us.usws.model.domain.system.User;
import com.us.usws.model.flat.system.DODept;
import com.us.usws.service.cud.DeptCUDService;
import com.us.usws.service.ro.UserROService;

public class DeptCUDServiceImpl implements DeptCUDService {

	@Inject
	private UserROService userService;

	private DeptDAO dao = new DeptDAO();

	@Override
	public String add(DODept dept) {
		return dao.insertDept(dept);
	}

	@Override
	public void update(DODept dept) {
		dao.updateDept(dept);
	}

	@Override
	public void delete(String uid) {
		dao.updateIsValid(uid, SiteConstants.STR_BOOLEAN_NO);
	}

	@Override
	public void undelete(String uid) {
		dao.updateIsValid(uid, SiteConstants.STR_BOOLEAN_YES);
	}

	@Override
	public void hardDelete(String uid) throws DataConflictException {
		List<User> users = userService.getUsersByDept(uid);

		if (!users.isEmpty()) {
			throw new DataConflictException("Still has users, please remove all the users before delete Dept.");
		}

		dao.deleteDept(uid);
	}

}
